import EventEmitter from 'eventemitter3'

export class WXMiniProgramProxy extends EventEmitter {
  name: string

  constructor() {
    super()

    this.name = 'WXMiniProgramBridge'
  }

  async navigateTo(wxPageURL: string) {
    return new Promise((resolve, reject) => {
      wx.miniProgram.getEnv((res: any) => {
        const currentIsMiniProgramEnv = res?.miniprogram

        if (currentIsMiniProgramEnv) {
          wx.miniProgram.navigateTo({ url: wxPageURL }).then(resolve)

          return
        }

        reject(new Error('当前环境不是小程序环境'))
      })
    })
  }
}
